import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:p_location/alarm/constant/alarm_http_url.dart';
import 'package:p_location/alarm/obj/area/obj_one_key_detail.dart';
import 'package:p_location/alarm/res/alarm_area_item.dart';
import 'package:p_location/alarm/res/area/res_area_one_key_detail.dart';
import 'package:p_location/alarm/utils/trace_judger.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/no_result_widget.dart';
import 'package:p_location/common/widget/tag_view.dart';
import 'package:p_location/common/widget/vertical_info_item.dart';
import 'package:p_location/map/res/search_person_detail.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_constants.dart';
import 'package:p_location/net/net_listener_impl.dart';
import 'package:p_location/utils/navigator_util.dart';
import 'package:p_location/utils/net/search_person_util.dart';
import 'package:p_location/utils/string_util.dart';

class PageAlarmAreaPersonDetail extends StatefulWidget {

  final AlarmAreaItem item;

  final int id;

  final String typeName;

  final String info;

  final String time;

  //报警状态
  final String status;

  PageAlarmAreaPersonDetail(this.item,this.id, this.typeName, this.info, this.time,this.status);

  @override
  _PageAlarmAreaPersonDetailState createState() =>
      _PageAlarmAreaPersonDetailState();
}

class _PageAlarmAreaPersonDetailState extends State<PageAlarmAreaPersonDetail> {
  String errMsg = "";
  bool isLoading = true;
  ObjOneKeyDetail oneKeyDetail;

  SearchPersonDetail fullDetail;

  @override
  void initState() {
    super.initState();
    if (widget.item != null && StringUtil.isNotEmpty(widget.item.sn)) {
      _getFullInfoBySn(widget.item.sn);
    } else {
      _getDetail();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          brightness: Brightness.dark,
          centerTitle: true,
          title: Text("报警详情"),
          leading: IconButton(
              tooltip: '返回',
              icon: Icon(Icons.arrow_back_ios, color: Colors.white),
              onPressed: () {
                Navigator.of(context).pop();
              }),
        ),
        body: isLoading
            ? _loadingView
            : StringUtil.isEmpty(errMsg)
                ? _contentView
                : _exceptionView);
  }

  get _loadingView => Center(child: CircularProgressIndicator());

  get _contentView => ListView(
        children: [
          VerticalInfoItem("事件", widget.info),
          VerticalInfoItem("报警类型", widget.typeName),
          VerticalInfoItem("报警时间", widget.time),
          VerticalInfoItem("报警状态", widget.status),
          VerticalInfoItem("报警区域", oneKeyDetail.areaName),
          //可以封装
           IntrinsicHeight(
            child:
               Row(
                mainAxisSize:MainAxisSize.max,
                children: [
                Expanded(child: VerticalInfoItem("报警当事人", oneKeyDetail.name),flex: 2,),
                Expanded(child:
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border(bottom: BorderSide(color: Colors.grey, width: 0.4))),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Offstage(
                                offstage: widget.status != '报警中' || TraceJudger.isHide(widget.typeName),
                                child: TagView(
                                  "追踪",
                                  paddingLeft: 15,
                                  paddingRight: 15,
                                  paddingTop: 1,
                                  paddingBottom: 1,
                                  radius: 5,
                                  strokeColor: Color.fromARGB(255, 56, 160, 253),
                                  textColor: Color.fromARGB(255, 56, 160, 253),
                                  fillColor: Colors.white,
                                  onPress: (){
                                    if (fullDetail == null) {
                                      fullDetail = SearchPersonDetail();
                                      fullDetail.id = widget.item.id;
                                      fullDetail.name =
                                          widget.item.personNameOrAreaName;
                                      fullDetail.sn = widget.item.sn;
                                      fullDetail.typeName = widget.item.typeName;
                                    }

                                    NavigatorUtil.jumpToLocationPageWithTarget(
                                        context, fullDetail);
                                  },
                                ),
                              )
                            ],
                          ),
                        )
                ),
              ],
            ),
          ),
          VerticalInfoItem("部门", oneKeyDetail.departName),
          VerticalInfoItem("部门负责人", oneKeyDetail.departChiefName)
        ],
      );

  get _exceptionView => NoResultWidget('images/img_no_accident.png', errMsg);

  _getDetail() async {
    Map<String, dynamic> params = {};
    params["id"] = widget.id;
    await DioHttpClient.requestPost(
        context,
        _getUrl(),
        params,
        NetListenerImpl(onSuccess, onError),
        contentType: NetConstants.POST_TYPE_FORM);
  }

  _getUrl(){
    if(widget.typeName == "一键报警"){
      return AlarmHttpUrl.ALARM_ONE_KEY_DETAIL_URL;
    }else if(widget.typeName == "越界报警"){
      return AlarmHttpUrl.ALARM_OVER_BOUNDARY_DETAIL_URL;
    }else if(widget.typeName == "滞留报警"){
      return AlarmHttpUrl.ALARM_RETENTION_URL;
    }else if(widget.typeName == "静止报警"){
      return AlarmHttpUrl.ALARM_STATIC_URL;
    }
  }

  void onSuccess(Response response) {
    setState(() {
      if (response != null && response.data != null) {
        ResAreaOneKeyDetail res = ResAreaOneKeyDetail.fromJson(response.data);
        if (res != null && res.data != null) {
          oneKeyDetail = res.data;
        }
        isLoading = false;
      }
    });
  }

  void onError(CommonRes errorRes) {
    setState(() {
      errMsg = errorRes.msg ?? "请求异常";
      isLoading = false;
    });
  }

  _getFullInfoBySn(String keyword) {
    SearchPersonUtil searchPersonUtil =
    SearchPersonUtil(keyword, (List<SearchPersonDetail> _detailList) {
      if (_detailList != null && _detailList.length > 0) {
        SearchPersonDetail searchPersonDetail = _detailList[0];
        if (searchPersonDetail != null) {
          fullDetail = searchPersonDetail;
        }
      }
      _getDetail();
    }, () {
      _getDetail();
    });
    searchPersonUtil.requestSearchPerson(context);
  }
}
